import operate from '@dfeidao/atom-nodejs/db/operate';
import query from '@dfeidao/atom-nodejs/db/query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';
interface Message {
	filter: {

		hidden_source_feature_no: string;
		hidden_source_feature_code: string;
		hidden_source_feature_name: string;
		control_data: string;
	};
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	// params: {};
	// headers: {};
	// captcha: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:f:\实习\kh\src\wyf\zj-000005\s002,action_id:' + action_id);
	// 获取系统当前时间
	const now_date = new Date().getTime(); // 1551167783406
	// 查询初始化时的数据（参考第一个项目的zj-000005中的s002)
	const { prepare, exec: e } = query(action_id, session_id, systemid);
	prepare('hidden_source_feature_set', ['hidden_source_feature_code', 'hidden_source_feature_name', 'control_data', 'hidden_source_feature_no'], { productid, hidden_source_feature_no: message.filter.hidden_source_feature_no }, 1, 1, [], []);
	// 修改
	const { exec, update, insert } = operate(action_id, session_id, systemid);
	update('hidden_source_feature_set', { productid, hidden_source_feature_no: message.filter.hidden_source_feature_no }, { hidden_source_feature_name: message.filter.hidden_source_feature_name, hidden_source_feature_code: message.filter.hidden_source_feature_code, control_data: message.filter.control_data });

	const [data] = await e<{ hidden_source_feature_code: string; hidden_source_feature_name: string; control_data: string; }>();

	data.forEach((n) => {
		if (n.hidden_source_feature_name !== message.filter.hidden_source_feature_name) {
			insert('modify_record', { hidden_source_feature_no: message.filter.hidden_source_feature_no, change_field: "隐患源特性名称", change_before: n.hidden_source_feature_name, change_after: message.filter.hidden_source_feature_name, change_personnel: 'weiwei', change_date: now_date, productid });
		}
		if (n.hidden_source_feature_code !== message.filter.hidden_source_feature_code) {
			insert('modify_record', { hidden_source_feature_no: message.filter.hidden_source_feature_no, change_field: "隐患源特性代码", change_before: n.hidden_source_feature_code, change_after: message.filter.hidden_source_feature_code, change_personnel: 'weiwei', change_date: now_date, productid });
		}
		if (n.control_data !== message.filter.control_data) {
			insert('modify_record', { hidden_source_feature_no: message.filter.hidden_source_feature_no, change_field: "控制数据", change_before: n.control_data, change_after: message.filter.control_data, change_personnel: 'weiwei', change_date: now_date, productid });
		}
	});


	const r = await exec();

	log('Service end path:f:\实习\kh\src\wyf\zj-000005\s002,action_id:' + action_id);
	return {
		data: r
	};
}
